package by.grsu.hdm;

import java.util.ArrayList;
import java.util.List;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.devices.NetworkMonitor;
import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.devices.WashingMachine;
import by.grsu.hdm.exceptions.IllegalInternalStateException;
import by.grsu.hdm.media.MiniMediaPlayer;
import by.grsu.hdm.media.RemoteControl;
import by.grsu.hdm.model.IDevice;
import by.grsu.hdm.model.IHomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.reporting.TextReportBuilder;
import by.grsu.hdm.reporting.TrafficReportData;

public class HomeDeviceManagerStarter {

	public static void main(String[] args) {
		
		List<IHomeDevice> devices = new ArrayList<IHomeDevice>();
		
		MediaCenter media = new MediaCenter("MEDIA#132456");
		UsbFlashDrive ufd = new UsbFlashDrive("FLASH-4GB");
		
		try {
			media.getUsbPort().connectDevice(ufd);
		} catch (IllegalInternalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		devices.add(media);
		devices.add(new WashingMachine("WASHING#45"));
		
		// turn all devices on..
		for (IHomeDevice device : devices) {
			device.turnOn();
		}
		
		// check for connected devices
		for (IDevice device : devices) {
			if (device instanceof IUsbSupport) {
				IUsbPort usb = ((IUsbSupport)device).getUsbPort();
				if (usb.hasConnectedDevice()) {
					IDevice usbDevice = usb.getConnectedDevice();
					
					if (usbDevice != null)
					System.out.println(usbDevice.getDeviceId() + " is connected to " + device.getDeviceId());
				}
			}
		}
		
		
		// register devices for network monitoring
		NetworkMonitor monitor = new NetworkMonitor();
		
		for (IDevice device : devices) {
			if (device instanceof INetworkAvailable) {
				monitor.registerDevice((INetworkAvailable)device);
			}
		}
		
		
		
		
		TrafficReportData reportData = monitor.getNetworkStatistics();
		
		TextReportBuilder text = new TextReportBuilder(reportData);
		String report = text.buildReport();
		
		System.out.println(report);
		
		
		media.getUsbPort().disconnectDevice();
		
		if(media.getUsbPort().getConnectedDevice() == null){
			System.out.println(ufd.getDeviceId() + " is disconnected from " + media.getDeviceId());
		}
		
		// turn all devices off..
		for (IHomeDevice device : devices) {
			device.turnOff();
		}
		
		RemoteControl remoteControl = new RemoteControl();
		
		try {
			remoteControl.play();
		} catch (IllegalInternalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		MiniMediaPlayer mediaPlayer = new MiniMediaPlayer();
		mediaPlayer.playMp3();
		
		System.out.println("Format is "+mediaPlayer.getFormat());
		
		remoteControl.pause();
		
		mediaPlayer.playerStatus();
		
		
	}

}
